from dateutil.relativedelta import relativedelta
import requests
import execjs
import time
from datetime import datetime, timedelta

now = datetime.now()
# 获取今天的日期，并设置到23:59:59
begintime = datetime(now.year, now.month, now.day, 23, 59, 59)

# 计算6个月前的日期，并设置到23:59:59
six_months_ago = (begintime - relativedelta(months=6)).replace(hour=0, minute=0, second=0)
# 计算6个月前的日期
six_months_ago = begintime - timedelta(days=6 * 30)  # 大约6个月

# 格式化日期
begintime_str = begintime.strftime('%Y-%m-%d %H:%M:%S')
endtime_str = six_months_ago.strftime('%Y-%m-%d %H:%M:%S')
print(begintime_str,endtime_str)
t = int(time.time() * 1000)
json_data = {
    'pageNo': 2,
    'pageSize': 20,
    'total': 3520,
    'AREACODE': '',
    'M_PROJECT_TYPE': '',
    'KIND': 'GCJS',
    'GGTYPE': '1',
    'PROTYPE': '',
    'timeType': '6',
    'BeginTime': '2024-02-22 00:00:00',
    'EndTime': '2024-08-22 23:59:59',
    'createTime': [],
    'ts': t,
}
# json_data = {
#     'type': '12',
#     'IS_IMPORT': 1,
#     'pageSize': 3,
#     'ts': t,
# }
with open('fjsggzy.js', 'r', encoding='utf-8') as f:
    js_code = f.read()
js_compile = execjs.compile(js_code)
sign = js_compile.call('get_sign', json_data)
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
    'portal-sign': sign,
}

# response = requests.post('https://ggzyfw.fujian.gov.cn/FwPortalApi/Article/PageList', headers=headers, json=json_data)
response = requests.post('https://ggzyfw.fujian.gov.cn/FwPortalApi/Trade/TradeInfo', headers=headers, json=json_data)
decrypt_data = js_compile.call('decrypt_data', response.json()['Data'])
print(decrypt_data)
